<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class UserController extends AdminController{
    
    
    public function actionIndex(){
        
        $User = new User();
        $User->unsetAttributes();
        
        
        if(isset($_GET['User'])){          
            $User->attributes = $_GET['User'];
        }
        
        
         $this->render('index',array(
          'User'=>$User  
        ));
        
    }
    
    
    public function actionAdd(){
        
       $User = new User();
       $User->unsetAttributes();
       
        $transaction  =Yii::app()->db->beginTransaction();
       
       if(isset($_POST['User'])){

           $User->attributes = $_POST['User'];
           $User->reg_phone_type =3;
           $User->ctime =time();
           $User->status=1;
           $AvatarUrlTmp=  BootExtend::UploadPathPro($_POST['User']['avatar_url'],$User,'upload/avater');
            if($AvatarUrlTmp){
                $User->avatar_url = $AvatarUrlTmp;
            }else{
                Yii::app()->jump->error('头像储存失败!');
                $this->refresh();
            }        

           if($User->save()){
                $transaction->commit();
                Yii::app()->jump->success("添加成功",yii::app()->createUrl('xyhadm/user/index'));
           }else{
                var_dump($User->getErrors()) ;exit();
           } 
       }
        $this->render('add',array(
          'User'=>$User  
        ));

    }
    
    public function actionEdit($id){
        
       $UserData =  $this->getPkModelData($id,'User');
       
       
       if(isset($_POST['User'])){
           
           $UserData->attributes = $_POST['User'];
           
          
           $UserData->mtime =time();
           $AvatarUrlTmp=  BootExtend::UploadPathPro($_POST['User']['avatar_url'],$User,'upload/avater');
            if($AvatarUrlTmp){
                $User->avatar_url = $AvatarUrlTmp;
            }else{
                Yii::app()->jump->error('头像储存失败!');
                $this->refresh();
            } 
           
           if($UserData->save()){
                     // Yii::app()->user->setFlash('addform', "修改成功！");
                      Yii::app()->jump->success("修改成功",yii::app()->createUrl('xyhadm/user/index'));
                      //$this->refresh();
           }else{
                Yii::app()->jump->error("修改失败". var_dump($UserData->getErrors()));
           } 
       }
        $this->render('add',array(
          'User'=>$UserData  
        ));

    }
    
    
      /**
     * 根据主键获取数据
     * @param int    $id    主键ID
     * @param string $model    模型名
     * return  obj
     */
    
    private function getPkModelData($id,$model){
        
        $ModelData = $model::model()->findByPk($id);
        if($ModelData){
            
            return $ModelData;
        }
        else{
          
            throw new CHttpException(404,"'.$model.'表无数据");
        }
         
    }
    
     public function actionDelete($id)
    {
        if(Yii::app()->request->isAjaxRequest){
            if($id){
                $User = User::model()->findByPk($id);
                $User->is_del=1;
                $User->status=2;
               // $StoreData->name=$StoreData->name.'-'.date('Y-m-d H:i:s',time());
                if($User->save(FALSE))
                {
                    echo '删除成功';
                }else{
                   echo FALSE;
                   
                }
            }
            
        }
    }
    
    
      public function actionUploads()
    {
        
         $imgUrl ='http://ti.211xyh.com';
         $imgUrlPath=Yii::app()->basePath.'/../';
         
        if(Yii::app()->request->isAjaxRequest)
        {
            $is_thum =0; //缩略图判断值   
            
            switch ($_POST['img_type']){
                
                    case  'file_upload_ws':
                           $targ_w =100; $targ_h =100; 
                           break;
//                    case  'file_upload_ws_min':
//                           $targ_w =100; $targ_h =100; 
//                           $is_thum=1;
//                           break;
//                    case  'file_upload_ws_vote':
//                           $targ_w =750; $targ_h =445; 
//                           break;
//                    case  'file_upload_ws_min_vote':
//                           $targ_w =100; $targ_h =100; 
//                           $is_thum=1;
            }

          
           //$url_root = explode($_SERVER['HTTP_HOST'],$_POST['input_img_url']);
           //$src =  $_SERVER['DOCUMENT_ROOT'] . $url_root[1];
           
           $url_root = explode($imgUrl,$_POST['input_img_url']); //服务器特用
           $src =  $imgUrlPath . $url_root[1];
            
            
           
           $pathinfos = pathinfo($url_root[1]);
           $src_path = $pathinfos['extension'];
         
           //文件判断
          switch ($src_path)
          {
            case 'jpg':
                    $img_r = imagecreatefromjpeg($src);
                break;
            case 'jpeg':
                    $img_r = imagecreatefromjpeg($src);
                break;
            case 'png':
                    $img_r = imagecreatefrompng($src);
                break;

            default:
                    echo 4;
               exit;
                break;
           }

           $dst_r = ImageCreateTrueColor($targ_w,$targ_h);
           $pwd_ture=imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
           $targ_w,$targ_h,$_POST['w'],$_POST['h']);
           @imagejpeg($dst_r,$src);
           
 
           //判断是否缩略图
           if($is_thum){
                //判断是否修改页
                if(isset($_POST['input_img_id']) && !empty($_POST['input_img_id']) &&$pwd_ture){
                    $sql_a = "update xyh_activity set min_banner_url='{$_POST['input_img_url']}' where id=".$_POST['input_img_id'];
                    $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
                } 
               
           }else{
               //判断是否修改页
                if(isset($_POST['input_img_id']) && !empty($_POST['input_img_id']) &&$pwd_ture){
                    $sql_a = "update xyh_activity set banner_url='{$_POST['input_img_url']}' where id=".$_POST['input_img_id'];
                    $pwd_ture = Yii::app()->db->createCommand($sql_a)->execute();
                }
           }
            
           //返回信息
            if($pwd_ture)
                echo $_POST['input_img_url'];
            else
                echo 3;
        }    
    }
    
    
    
    
}